package com.ease.core.components {
   import com.ease.core.events.PerspectiveEvent;
   import com.ease.core.managers.PerspectiveManager;

   import mx.controls.ToggleButtonBar;
   import mx.events.FlexEvent;

   public class PerspectiveBarBack extends ToggleButtonBar {
      private var iconMap:Object = { //
         };
      private var perspectiveManager:PerspectiveManager = PerspectiveManager.getInstance();
      [Bindable]
      public var perspectiveToggleItem:Array = [];

      public function PerspectiveBarBack() {
         super();
         this.styleName = "toggleButtonBar";
         this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
      }

      protected function onCreationComplete(event:FlexEvent):void {
         var activePerspectiveId:String = perspectiveManager.activePerspective;
         var perspectiveIds:Array = perspectiveManager.perspectiveIds;
         var xml:XML;
         var tempSelectedIndex:int = 0;

         for (var ii:int = 0; ii < perspectiveIds.length; ii++) {
            xml = perspectiveManager.getPerspective(perspectiveIds[ii]);

            perspectiveToggleItem.push({label: xml.@name.toString(), perspectiveId: xml.@id.toString(), icon: Class(systemManager.getDefinitionByName(xml.@icon.toString()))});
            if (activePerspectiveId == xml.@id) {
               tempSelectedIndex = ii;
            }

         }
         this.dataProvider = perspectiveToggleItem;
         this.selectedIndex = tempSelectedIndex;
      }

      private function onPerspectiveOpened(event:PerspectiveEvent):void {
         var len:int = perspectiveToggleItem.length;
         var item:Object;
         var tempSelectedIndex:int = 0;
         for (var i:int = len - 1; i >= 0; i--) {
            item = perspectiveToggleItem[i];
            if (event.perspectiveId == item.perspectiveName) {
               tempSelectedIndex = i;
               break;
            }
         }
         this.selectedIndex = tempSelectedIndex;
      }

   }
}